<?php

function getCateTree() {
	$ci=& get_instance();
	$parent = 0;
	$children = array();
	$i=0;
	$children = getChildren($children,$parent);
	foreach($children as $cat) {
		if(checkCateIsParent($cat->cate_id)) {
			$cat->is_parent = 1;
		} else {
			$cat->is_parent = 0;
		}
	}
	
	return $children;
}

function getChildren($list=array(),$parent=0,$level = 0) {
	$ci=& get_instance();
	$ci->db->order_by("cate_name", "asc"); 
	$query = $ci->db->get_where('ci_categories', array('cate_parent' => $parent));
	if($query->num_rows) {
		$result = $query->result();
		foreach($query->result() as $xyz) {
			$xyz->level = $level;
			$list[] = $xyz;
			$list 	= getChildren($list,$xyz->cate_id,$level+1);
		}
	} else {
		return $list;
	}
	return $list;
}

function checkCateIsParent($catId){
	$ci=& get_instance();
	$query = $ci->db->get_where('ci_categories', array('cate_parent' => $catId));
	return $query->num_rows;
}